package com.zygh.questionnairesurvey.controller;

import com.alibaba.fastjson.JSONObject;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.UserMenu;
import com.zygh.questionnairesurvey.server.UserMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/UserMenu")
@Api(tags = "菜单管理")
public class UserMenuController {

    @Resource
    private UserMenuService userMenuService;

    @GetMapping("list")
    @ApiOperation(value = "查询所有菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", required = true)
    })
    public CommonResult listAll(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userMenuService.listAll(pageNum, pageSize);
    }


    @GetMapping("getById")
    @ApiOperation(value = "根据id查询菜单信息")
    public CommonResult getById(Integer userMenuId) {
        return userMenuService.getById(userMenuId);
    }


    @PostMapping("insert")
    @ApiOperation(value = "新增菜单")
    public CommonResult insert(@RequestBody UserMenu userMenu) {
        return userMenuService.insert(userMenu);
    }


    @PostMapping("update")
    @ApiOperation(value = "修改菜单")
    public CommonResult update(@RequestBody UserMenu userMenu) {
        return userMenuService.update(userMenu);
    }


    @PostMapping("delete")
    @ApiOperation(value = "删除菜单")
    public CommonResult delete(@RequestBody String id) {
        String str = JSONObject.parseObject(id).get("id").toString();
        Integer new_id = Integer.parseInt(str);
        return userMenuService.delete(new_id);
    }

}